package com.eas.ereceipt.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.eas.ereceipt.R;
import com.eas.ereceipt.bean.GroupSlidingMenu;
import com.eas.ereceipt.bean.ItemSlidingMenu;
import com.eas.ereceipt.util.SlidingMenuConfig;

public class SlidingMenuAdapter extends BaseExpandableListAdapter {
	private LayoutInflater inflater;

	public SlidingMenuAdapter(Context c) {
		inflater = LayoutInflater.from(c);
	}

	@Override
	public Object getChild(int groupPos, int pos) {
		return SlidingMenuConfig.getGroups().get(groupPos).getItems().get(pos);
	}

	@Override
	public long getChildId(int groupPos, int pos) {
		return SlidingMenuConfig.getGroups().get(groupPos).getItems().get(pos)
				.getId();
	}

	@Override
	public View getChildView(int groupPos, int pos, boolean isLastChild,
			View convertView, ViewGroup parrent) {
		ItemViewHolder holder;
		if (convertView == null){
			holder = new ItemViewHolder();
			convertView = inflater.inflate(R.layout.item_sliding_menu, parrent, false);
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
			holder.imgViewIcon = (ImageView) convertView.findViewById(R.id.imgViewIcon);
			convertView.setTag(holder);
		} else 
			holder = (ItemViewHolder) convertView.getTag();
		
		ItemSlidingMenu i = SlidingMenuConfig.getGroups().get(groupPos).getItems().get(pos);
		holder.tvTitle.setText(i.getTitle());
		try {
			holder.imgViewIcon.setImageResource(i.getIconResID());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPos) {
		return SlidingMenuConfig.getGroups().get(groupPos).getItems().size();
	}

	@Override
	public Object getGroup(int groupPos) {
		return SlidingMenuConfig.getGroups().get(groupPos);
	}

	@Override
	public int getGroupCount() {
		return SlidingMenuConfig.getGroups().size();
	}

	@Override
	public long getGroupId(int groupPos) {
		return SlidingMenuConfig.getGroups().get(groupPos).getId();
	}

	@Override
	public View getGroupView(int groupPos, boolean isExpanded, View convertVew,
			ViewGroup parrent) {
		GroupViewHolder holder;
		if (convertVew == null){
			holder = new GroupViewHolder();
			convertVew = inflater.inflate(R.layout.header_sliding_menu, parrent, false);
			holder.tvTitle = (TextView) convertVew.findViewById(R.id.tvTitle);
			convertVew.setTag(holder);
		} else
			holder = (GroupViewHolder) convertVew.getTag();
		
		GroupSlidingMenu group = SlidingMenuConfig.getGroups().get(groupPos);
		
		holder.tvTitle.setText(group.getTitle());
		
		return convertVew;
	}

	@Override
	public boolean hasStableIds() {
		
		return false;
		
	}

	@Override
	public boolean isChildSelectable(int groupPos, int pos) {
		return true;
	}

	class GroupViewHolder {
		TextView tvTitle;
	}

	class ItemViewHolder {
		TextView tvTitle;
		ImageView imgViewIcon;
	}

}
